layui.config({
	base: "/web/cdacsrecord/lib/layui/"
	// base: "/lib/layui/"
}).extend({
	xmSelect: 'xm-select'
});
layui.use(['element','table','layer','form','xmSelect'], function(){
  var table = layui.table;
  var layer = layui.layer;
  var form=layui.form
  , $ = layui.jquery;
	var util = layui.util;
	var demo = xmSelect.render({
		el: '#ssqy',
		model: { label: { type: 'text' } },
		radio: true,
		clickClose: true,
		tree: {
			show: true,
			strict: false,
			expandedKeys: [ -1 ],
		},
		height: 'auto',
		data:[]
	});
	$.post('/acsCommunity/getAreaTree',{},function (res) {
		if(res.resultCode === '000'){
			demo.update({
				data: res.resultContent,
				autoRow: true,
			});
		}else{
			layer.msg(res.resultDesc);
		}
	})
	
  table.render({
	    elem: '#basicBlockTable',
	    height: 'full-150',
	    url:  "/acsCommunity/list", //数据接口
	    method: 'post',
	    request:{
			page: 'pageNum', //页码的参数名称，默认：page
			limit: 'pageSize' //每页数据量的参数名，默认：limit
	    },
	    where: {
			name: ""
		},
	    page: true, //开启分页
	    cols: [[ //表头
	    	{type:'numbers'}
			,{field: 'companyId', title: 'ID',width:180,align:'center' ,hide:true}
	    	,{field: 'name', title: '小区名称',width:120,align:'center' 	}
	    	,{field: 'code', title: '小区编号',width:120,align:'center' 	}
	    	,{field: 'areaName', title: '所属区域',width:120,align:'center' }
	    	,{field: 'linkman', title: '联系人',width:130, align:'center' }
	    	,{field: 'address', title: '地址',width:130, align:'center' }
	    	,{field: 'contactNumber', title: '电话',width:120, align:'center'  }
	    	,{field: 'email', title: '邮箱',width:120, align:'center'  }
	    	,{field: 'remark', title: '备注',width:130, align:'center'  }
	        , {field: 'right', title: '操作', align:'center',width:200,templet:function(data){
	    	  var op = '<a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>';
	    	  op+='<a class="layui-btn layui-btn-xs" lay-event="delete">删除</a>';
	    	  return op;
	      }
	      }
	    ]]

	  });

    //添加楼栋信息
	addBlock = function () {
		$("#block_up").hide();
		$("#block_form").show();
		$("#block_form").attr("disabled", false);
		$("#block_form").attr("class", "layui-btn");
		$("#product_form")[0].reset();
		layer.open({
			type: 1
			, title: '新增'
			, shadeClose: true
			, area: ['60%', '80%']
			, content: $('#blockAdd')
		});
	};

	//操作列
    table.on('tool(basicBlockTable)', function(obj){
	    var data = obj.data;
	    switch (obj.event) {
			case 'delete':
				layer.confirm('是否确认删除？', {
					btn: ['确认', '返回'] //按钮
				}, function () {
					var index = layer.load(2, {
						shade: [0.4, '#000'],
						content: '提交中...',
						success: function (layero) {
							layero.find('.layui-layer-content').css({
								'padding-top': '40px',
								'width': '200px'
							});
						}
					});
					$.post('/acsCommunity/delete',{id:data.companyId},function (res) {
						if(res.resultCode === '000'){
							layer.close(index);
							layer.msg('删除成功',{
								time:500,
								end:function () {
									table.reload('basicBlockTable')
								}
							})
						}else{
							layer.msg(res.resultDesc);
							layer.close(index);
						}
					})
				});
				break;
			case 'edit':
				$("#block_up").show();
				$("#block_form").hide();
				$("#block_up").attr("disabled", false);
				$("#block_up").attr("class", "layui-btn");
				$("#companyId").val(data.companyId);
				form.val('product_form',{
					// 'id': data.companyId,
					'name': data.name,
					'address':data.address,
					'linkman':data.linkman,
					'contactNumber': data.contactNumber,
					'remark': data.remark,
					'email': data.email
				});
				form.render();
				layer.open({
					type: 1
					, title: '编辑信息'
					, shadeClose: true
					, area: ['58%', '80%']
					, content: $('#blockAdd')
				});
				break;

		}
	});

	// 同步数据
	$("#sync").on('click', function (){
		layer.confirm('同步数据耗时较长，是否继续？', {
			btn: ['确认', '取消'] //按钮
		}, function () {
			var index = layer.load(2, {
				shade: [0.4, '#000'],
				content: '同步中...',
				success: function (layero) {
					layero.find('.layui-layer-content').css({
						'padding-top': '40px',
						'width': '200px'
					});
				}
			});
			$.post('/acsCommunity/syncData',{},function (res) {
				if(res.resultCode === '000'){
					layer.msg(res.resultDesc,{
						time:500,
						end:function () {
							reloadCommunity();
						}
					})
				}else{
					layer.msg(res.resultDesc);
				}
				layer.close(index);
			});
		})
	});

	//提交小区信息表单
	form.on('submit(block_form)',function(data){
		var ssqy = demo.getValue();
		if(!ssqy || ssqy.length === 0){
			layer.msg("请选择区域",{icon:5});
			return false;
		}
		var jsonArr = JSON.stringify(ssqy);
		var kv = jsonArr.slice(1,jsonArr.length-1)
		var kvData = JSON.parse(kv);
		data.field.areaId = $.trim(kvData.value);
		data.field.areaName = $.trim(kvData.name);
		$("#block_form").attr("disabled", true);
		$("#block_form").attr("class", "layui-btn layui-btn-disabled");
		var inFun = 0;
		layer.confirm('是否确认提交小区信息？', {
			btn: ['确认', '返回']
		}, function () {
			if(inFun === 1){
				return;
			}
			inFun = 1;
			var index = layer.load(2, {
				shade: [0.4, '#000'],
				content: '提交中...',
				success: function (layero) {
					layero.find('.layui-layer-content').css({
						'padding-top': '40px',
						'width': '200px'
					});
				}
			});
			$.post("/acsCommunity/saveOrUpdate", data.field, function (data) {
				layer.closeAll();
				if(data.resultCode === "000"){
					layer.msg('提交成功',{
						time:500,
						end:function () {
							reloadBlockList();
						}
					})
				}else{
					layer.msg(data.resultDesc);
					$("#block_form").attr("disabled", false);
					$("#block_form").attr("class", "layui-btn");
				}
			})
		}, function () {
			layer.closeAll();
			return false;
		});
		return false;
	});

	//更新小区信息
	form.on('submit(block_up)',function(data){
		var ssqy = demo.getValue();
		if(!ssqy || ssqy.length === 0){
			layer.msg("请选择区域",{icon:5});
			return false;
		}
		var jsonArr = JSON.stringify(ssqy);
		var kv = jsonArr.slice(1,jsonArr.length-1)
		var kvData = JSON.parse(kv);
		data.field.areaId = $.trim(kvData.value);
		data.field.areaName = $.trim(kvData.name);
		$("#block_up").attr("disabled", true);
		$("#block_up").attr("class", "layui-btn layui-btn-disabled");
		var inFun = 0;
		layer.confirm('是否确认更新小区信息？', {
			btn: ['确认', '返回']
		}, function () {
			if(inFun === 1){
				return;
			}
			inFun = 1;
			var index = layer.load(2, {
				shade: [0.4, '#000'],
				content: '提交中...',
				success: function (layero) {
					layero.find('.layui-layer-content').css({
						'padding-top': '40px',
						'width': '200px'
					});
				}
			});
			$.post("/acsCommunity/saveOrUpdate", data.field, function (data) {
				layer.closeAll();
				if(data.resultCode === "000"){
					layer.msg('更新成功',{
						time:500,
						end:function () {
							reloadBlockList();
						}
					})
				}else{
					layer.close(index);
					layer.msg(data.resultDesc);
					$("#block_up").attr("disabled", false);
					$("#block_up").attr("class", "layui-btn");
				}
			})
		}, function () {
			layer.closeAll();
			return false;
			$("#block_up").attr("disabled", false);
			$("#block_up").attr("class", "layui-btn");
		});
		return false;
	});

	function reloadBlockList(){
		reloadCommunity();
	}

	//查询楼栋
	form.on('submit(search_block)',function () {
		//执行重载
		table.reload('basicBlockTable', {
			page: {
				curr: 1 //重新从第 1 页开始
			}
			,where: {
				name: $('#searchBlockName').val(),
			}
		});
		return false;
	});

});
function reloadCommunity() {
	$(window.parent.document).find("#reload").click();
}
function reloadCommunity2(){
	location.reload();
}